package com.lwb.ztx.main.widget;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.ProfileBean;
import com.lwb.ztx.bean.Profiles;
import com.lwb.ztx.main.presenter.LoadingPresenter;
import com.lwb.ztx.main.presenter.LoadingPresenterImpl;
import com.lwb.ztx.main.view.LoadingView;
import com.lwb.ztx.utils.LogUtils;

import java.util.List;

public class LoadingActivity extends AppCompatActivity implements LoadingView {

    private LoadingPresenter presenter;
    private static final int totalStartTime = 1500;
    private long startTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startTime = System.currentTimeMillis();
        presenter = new LoadingPresenterImpl(this);
        setContentView(R.layout.activity_loading);
        presenter.getTotalProfile();
    }

    @Override
    public void onLoadingFinish(final List<ProfileBean> profileBeanList) {
        LogUtils.i("#################" + "load finish");
        if (System.currentTimeMillis() - startTime < totalStartTime) {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        Thread.sleep((totalStartTime - (System.currentTimeMillis() - startTime)));
                        goToMain(profileBeanList);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } else {
            goToMain(profileBeanList);
        }
    }
    private void goToMain(List<ProfileBean> profileBeanList) {
        Intent intent = new Intent(this, MainActivity.class);
        Profiles profiles = new Profiles();
        profiles.setProfileBeanList(profileBeanList);
        intent.putExtra("profiles", profiles);
        startActivity(intent);
        this.finish();
    }
}
